<?php

class Tag extends AppModel
{

    var $recursive = 2;
    
    var $hasAndBelongsToMany = array(
        'Artist' => array(
            'className' => 'Artist',
             'joinTable' => 'object_tags',
             'foreignKey' => 'tag_id',
            'associationForeignKey' => 'object_id',
            'unique' => true,
            'recursive' => 1,
            'fields' => array('Artist.name', 'Artist.id'),
            'conditions' => array('object_type' => 'artist')));
                
    function getPopularTags($limit = 50)
    {
        $tags = $this->query("
        SELECT COUNT(Object_tag.id) as num, Tag.tag 
        FROM tags AS Tag
        JOIN object_tags AS Object_tag ON Object_tag.tag_id = Tag.id  
        GROUP BY Tag.id  ORDER BY num DESC LIMIT $limit");
        
        return $tags;

    }
}

?>